package com.fcy.teamwork.business.checkapply

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fcy.teamwork.base.BaseActivity
import com.fcy.teamwork.bean.generateApply
import com.fcy.teamwork.databinding.ActivityCheckApplyBinding
import kotlinx.coroutines.launch

class CheckApplyActivity : BaseActivity<ActivityCheckApplyBinding>() {
    private val vm: CheckApplyVM by viewModels()
    private val adapter = ApplicationAdapter().apply {
        onAcceptClick = { data, position ->
            lifecycleScope.launch {
                data.jobApply.isResolved = 1
                notifyItemChanged(position)
                vm.processApply(data)
            }
        }
        onRefuseClick = { data, position ->
            lifecycleScope.launch {
                data.jobApply.isResolved = -1
                notifyItemChanged(position)
                vm.processApply(data)
            }
        }
    }

    override fun initBinding(): ActivityCheckApplyBinding =
        ActivityCheckApplyBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.rvApplications.also {
            it.layoutManager = LinearLayoutManager(this)
            it.adapter = adapter
        }
        binding.actionBar.apply {
            tvTitle.text = "申请列表"
            ibBack.setOnClickListener {
                finish()
            }
        }
        lifecycleScope.launch {
            adapter.submitData(vm.loadAllApply())
        }
    }
}